package plugins

import (
	"errors"

	"github.com/mojocn/base64Captcha"
)

type Captcha struct{}

func (p *Captcha) CreateCaptcha() map[string]interface{} {
	digitCaptcha := base64Captcha.NewDriverDigit(80, 240, 4, 0.7, 80)
	captcha := base64Captcha.NewCaptcha(digitCaptcha, base64Captcha.DefaultMemStore)
	id, b64s, _, err := captcha.Generate()
	if err != nil {
		panic(err)
	} else {
		return map[string]interface{}{"id": id, "b64s": b64s}
	}
}

func (p *Captcha) VerifyCaptcha(id string, content string) (bool, error) {
	storeValue := base64Captcha.DefaultMemStore.Get(id, true)
	isOk := (storeValue == content)
	var optionErr error
	if !isOk {
		optionErr = errors.New("验证码不正确！")
	}
	return isOk, optionErr
}
